﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using CalcSharp.Core;

namespace CalcSharp.MDIUI.Dialogs
{
    /// <summary>
    /// New Matrix Dialog
    /// </summary>
    public partial class NewMatrixDialog : Form
    {
        public NewMatrixDialog()
        {
            InitializeComponent();
            this.CbMatrixList.Items.Clear();
            this.CbMatrixList.Items.Add("-None-");
            this.CbMatrixList.Items.AddRange(Program.loaded.Mtrx_mem.keys);
            this.CbMatrixList.Text = "-None-";
        }

        private void BtnColorSelect_Click(object sender, EventArgs e)
        {
            if (ColorDialog.ShowDialog() == DialogResult.OK)
            {
                Int24 cnv = new Int24(ColorDialog.Color);
                this.TbInitial.Text = cnv.ToInt32().ToString();
            }
        }

        private void TbInitial_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(this.TbInitial.Text)) MiscFuncts.parsestr(this.TbInitial.Text);
            }
            catch (Exception)
            {
                MessageBox.Show("Value can't be parsed as a double number", "Initial value error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.TbInitial.Text = "0";
            }
        }

        private void CbMatrixList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (CbMatrixList.Text != "-None-" && !string.IsNullOrEmpty(CbMatrixList.Text)) this.groupBox2.Enabled = false;
            else this.groupBox2.Enabled = true;
        }

        private void CbMatrixList_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(CbMatrixList.Text) && CbMatrixList.Text != "-None-")
            {
                if (!Program.loaded.Mtrx_mem.isset(CbMatrixList.Text))
                {
                    MessageBox.Show("Invalid matrix name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        /// <summary>
        /// Gets or sets the row number
        /// </summary>
        public int Rows
        {
            get { return (int)this.NumRows.Value; }
            set { this.NumRows.Value = value; }
        }

        /// <summary>
        /// Gets or sets the column number
        /// </summary>
        public int Columns
        {
            get { return (int)this.NumColumns.Value; }
            set { this.NumColumns.Value = value; }
        }

        /// <summary>
        /// Gets or sets the initial value
        /// </summary>
        public double InitialValue
        {
            get { return MiscFuncts.parsestr(this.TbInitial.Text); }
            set { this.TbInitial.Text = value.ToString(); }
        }
    }
}
